import { Component, OnInit, EventEmitter, Output } from "@angular/core";
import {BizfileService} from '../../../../../@core/service/case-review/doc-service/api/bizfile.service'
import { ClueDataService } from "../../../../../@core/service/clue-service/clue-data.service";
import * as moment from 'moment';
@Component({
  selector: 'app-case-review-form',
  templateUrl: './case-review-form.component.html',
  styleUrls: ['./case-review-form.component.scss']
})
export class CaseReviewFormComponent implements OnInit {
  @Output('loadSpin') loadSpin = new EventEmitter();

  // 时间
  private date:any = new Date();
  //加载动画
  public loading:boolean = false;
  BizFile: any;


  public isReadonly:boolean = false;
  public doctype: string;     // 文书名称
  public condition: string;   // 新增or审批条件
    // 请求数据
    public filedata: any = {data: {stage: null}};
    public caseType: any;        // 案件环节
  //type选中值
  public treeValue: any;
  //区域数据
  public areaData: any;
  //省级联动
  public provinces: any[];
  //市级联动
  public cities: any[];
  //县级联动
  public counties: any[];
  //选择内容
  public addressDetails: any[] = [];
  public addressName: any[] = [];
  public sortValue: string = "0";
  public getSelectValue:string;
  // 登记机关
  public session:any = window.sessionStorage;
  public sessionDepart:any = JSON.parse(window.sessionStorage.getItem('organizations'))[0];
  public listOfOption:any;
  public expandKeys:any=['0-0'];
  public v1:any;
  public v2:any;
  public nodes:any;
  public value:any;
  // 送审时间
  private _submitTime:any = this.date;
    // 退卷日期
    private _unwindingTime:any = this.date;
    //审核时间
    private _reviewTime :any = this.date;
  //docid 如果是修改会传入docid
  public docid:string;
  //form内容
  public formJson: any = {
    aY:this.date.getFullYear(),
    aM : this.date.getMonth()+1,
    aD : this.date.getDate(),
    tY : this.date.getFullYear(),
    tM : this.date.getMonth()+1,
    tD : this.date.getDate()
  };
  // 送审时间
  public get submitTime(){
    return this._submitTime
  }
  public set submitTime(val){
    if(val){
      let date:Date = new Date(val);
      this.formJson.aY = date.getFullYear();
      this.formJson.aM = date.getMonth()+1;
      this.formJson.aD = date.getDate();
    }
    this._submitTime=val;
}
// 退卷日期
public get unwindingTime(){
  return this._unwindingTime
}
public set unwindingTime(val){
  if(val){
    let date:Date = new Date(val);
    this.formJson.tY = date.getFullYear();
    this.formJson.tM = date.getMonth()+1;
    this.formJson.tD = date.getDate();
  }
  this._unwindingTime=val;
}
// 审核时间
public get reviewTime(){
  return this._reviewTime
}
public set reviewTime(val){
  if(val){
    let date:Date = new Date(val);
    this.formJson.tY = date.getFullYear();
    this.formJson.tM = date.getMonth()+1;
    this.formJson.tD = date.getDate();
  }
  this._reviewTime=val;
}
  constructor(
    public bizService:BizfileService,
    public clueData: ClueDataService,
    ) {
      this.areaData = clueData.area;
     }

  ngOnInit() {
    let nodesData = JSON.parse(this.session.getItem('areaData'));
    this.nodes = this.DataProcess(nodesData, '0', '机构');
    console.log(nodesData)
  
    if(this.docid){ 
      this.bizService.queryBizFileByDocid(this.docid).subscribe(({data}:any)=>{
        this.loadSpin.emit(false);
        if(data){
        
          if(data.stage) this.getSelectValue = data.stage;
          this.BizFile = data;
          this.formJson = JSON.parse(data.formJson);
          // 送审时间回显
          this.submitTime =  this.formJson.aY+'-'+this.formJson.aM+'-'+this.formJson.aD;
          // 退卷时间回显
          this.unwindingTime =  this.formJson.tY+'-'+this.formJson.tM+'-'+this.formJson.tD;
          if(this.formJson.caseType){
            this.treeValue = JSON.parse(this.formJson.caseType);
          }
          if(this.formJson.fontName||this.formJson.name||this.formJson.idNum){
            this.sortValue = '1';
          }
        }
      })
    }else{
      for (let index = 0; index < this.sessionDepart.length; index++) {
        if(this.sessionDepart[index].orgTypeName=="机构"){
          this.v2 = this.sessionDepart[index].orgId;
          this.v1 = this.sessionDepart[index].areaParentId;
          this.expandKeys = [this.v1,this.v2]
          this.formJson.caseDept =  this.sessionDepart[index].orgName;
        }
      }
      console.log(this.expandKeys)
    }
  }
  //切换机构
getSelectedNodeLists(key){
  this.formJson.caseDept = key;
}
  // 将后台返回的数据处理成符合树形结构 
  DataProcess(data, parentId, type) {
    let arrayList = [];
    for(var i = 0; i < data.length; i++) {
      if (data[i]['orgTypeName'] == type && data[i]['orgParentId'] == parentId) {
        let obj = {
          'title': '', 
          'text': '',
          'value': '', 
          'key': '', 
          'parentId': '', 
          'children': []
        };
        obj.title = data[i]['orgName'] || "";
        obj.text = data[i]['orgName'] || "";
        obj.value = data[i]['orgId'] || "";
        obj.key = data[i]['orgName'] || "";
        obj.parentId = data[i]['orgParentId'] || "";
        obj.children = this.DataProcess(data, obj.value, '机构');
        if (obj.children.length <= 0) {
          obj['isLeaf'] = true;
        }
        arrayList.push(obj);
      }
    }
    return arrayList;
  }
    //tree选择
    treeOnChange(event) {
    }
  selectChange(event){
    this.getSelectValue = event;
  }

}
